//
//  QQMusicDataTool.swift
//  QQ音乐
//
//  Created by 曹均华 on 16/6/4.
//  Copyright © 2016年 曹均华. All rights reserved.
//

import UIKit

class QQMusicDataTool: NSObject {

    
    class func getMusicMs(result: (musicMs: [QQMusicModel]?)->())
    {
    
        // 发送一个网络请求, 获取
        // 异步,
     
        
        // 解析数据
        // 1. 加载plist
        guard let path = NSBundle.mainBundle().pathForResource("Musics.plist", ofType: nil) else {
            result(musicMs: nil)
            return
        }
        
        guard let dicArray = NSArray(contentsOfFile: path) else {
            result(musicMs: nil)
            return
        }
        
        
        // 2. 遍历字典
        var tempMusicMs = [QQMusicModel]()
        for dic in dicArray {
            // 3. 字典转模型
            let model = QQMusicModel(dic: dic as! [String : AnyObject])
            tempMusicMs.append(model)
        }

        // 4. 返回结果
        result(musicMs: tempMusicMs)
        
        
    }
    
    
    class func getLrcMs(lrcName: String) -> [QQLrcModel] {
        
        
        // 1. 根据歌词文件名称, 获取对应的歌词文件
        guard let path = NSBundle.mainBundle().pathForResource(lrcName, ofType: nil) else {
            return [QQLrcModel]()
        }
        
        // 2. 加载歌词内容(字符串)
        var lrcContent: String?
        do {
            lrcContent = try String(contentsOfFile: path)
        }catch {
            print(error)
            return [QQLrcModel]()
        }
        
        
        // 2. 解析歌词内容, -> [QQLrcModel]
        // 2.1 容错处理
        if lrcContent == nil {
            return [QQLrcModel]()
        }
        
        
        // 1. 分离每一行歌词
        let timeAndLrcStrs = lrcContent!.componentsSeparatedByString("\n")
        
        // 2. 把每一行歌词, 转成模型
        var lrcMs = [QQLrcModel]()
        for timeAndLrcStr in timeAndLrcStrs {
           

            // 1. 过滤垃圾数据
            if timeAndLrcStr.containsString("[ti:") ||  timeAndLrcStr.containsString("[ar:") || timeAndLrcStr.containsString("[al:") {
                
                continue
 
            }
          
            // QQLrcModel begintime endtime  lrccontent
            
            // 过滤 "["
           let resultTimeAndLrcStr = timeAndLrcStr.stringByReplacingOccurrencesOfString("[", withString: "")
            
            // 时间和内容数组
            let timeAndContentArray = resultTimeAndLrcStr.componentsSeparatedByString("]")
            
            if timeAndContentArray.count == 2 {
                let beginTime = timeAndContentArray[0]
                let lrcContent = timeAndContentArray[1]
                
//                print(beginTime, lrcContent)
                
                let lrcM = QQLrcModel()
                lrcMs.append(lrcM)
                lrcM.beginTime = QQTimeTool.getTimeInterval(beginTime)
                lrcM.lrcContent = lrcContent
                
                
            }
            
 
        }
        
        //
        let count = lrcMs.count
        for i in 0..<count {
            
            if i != count - 1
            {
                lrcMs[i].endTime = lrcMs[i+1].beginTime
            }
        }
        
        
        

        
        return lrcMs
        
    }
    
    
    class func getScrollRow(lrcMs: [QQLrcModel], currentTime: NSTimeInterval) -> (row:Int, lrcM: QQLrcModel) {
        
        let count = lrcMs.count
        for i in 0..<count {
            
            let lrcM = lrcMs[i]
            if currentTime >= lrcM.beginTime && currentTime < lrcM.endTime {
                
                return (i, lrcM);
            }
        }
        
        return (0, QQLrcModel());
  
        
    }
    
    
}
